package com.xlh.dao.conclude.helper;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.xlh.dao.conclude.ConcludeStudentMapper;
import com.xlh.dto.conclude.ConcludeParamDTO;
import com.xlh.pojo.conclude.ConcludeStudent;
import com.xlh.pojo.conclude.example.ConcludeStudentExample;
import com.xlh.vo.conclude.StudentListInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cfz
 */
@Repository
public class ConcludeStudentMapperHelper {

    @Autowired
    private ConcludeStudentMapper concludeStudentMapper;

    public List<ConcludeStudent> findByCondition(Long concludeId, String className){

        ConcludeStudentExample example = new ConcludeStudentExample();
        ConcludeStudentExample.Criteria criteria = example.createCriteria().andConcludeIdEqualTo(concludeId).andDeletedEqualTo(false);
        if(StrUtil.isNotBlank(className)){
            criteria.andClassNameLike("%"+className+"%");
        }
        example.setOrderByClause("class_name, sno");
        List<ConcludeStudent> concludeStudents = concludeStudentMapper.selectByExample(example);

        return concludeStudents;
    }

    /**
     * 查询目标表中关联的系统学生信息
     * @param concludeId
     * @return
     */
    public List<ConcludeStudent> findSysStudentInfos(Long concludeId){

        ConcludeStudentExample example = new ConcludeStudentExample();
        example.createCriteria().andConcludeIdEqualTo(concludeId).andDeletedEqualTo(false).andSysStuIdIsNotNull();
        example.setOrderByClause(" create_time, sno ");
        List<ConcludeStudent> concludeStudents = concludeStudentMapper.selectByExample(example);

        return concludeStudents;
    }



    public List<ConcludeStudent> findBySno(List<String> snos, Long concludeId){

        ConcludeStudentExample example = new ConcludeStudentExample();
        example.createCriteria().andSnoIn(snos).andConcludeIdEqualTo(concludeId).andDeletedEqualTo(false);
        List<ConcludeStudent> concludeStudents = concludeStudentMapper.selectByExample(example);

        return concludeStudents;
    }


    public void update(ConcludeStudent concludeStudents) {

        ConcludeStudentExample example = new ConcludeStudentExample();
        example.createCriteria().andIdEqualTo(concludeStudents.getId()).andDeletedEqualTo(false);
        concludeStudentMapper.updateByExampleSelective( concludeStudents, example);
    }


    public void deleteBatchById(List<ConcludeParamDTO> stuIds, String className) {

        List<Long> userIds = stuIds.stream().map(ConcludeParamDTO::getId).collect(Collectors.toList());
        ConcludeStudent record = new ConcludeStudent();
        record.setDeleted(true);
        ConcludeStudentExample example = new ConcludeStudentExample();
        ConcludeStudentExample.Criteria criteria = example.createCriteria().andIdIn(userIds);

        if(StrUtil.isNotBlank(className)){
            criteria.andClassNameEqualTo(className);
        }
        concludeStudentMapper.updateByExampleSelective( record, example);
    }


    /**
     * 批量更新操作
     * @param concludeStudents
     */
    public void updateBeach(List<ConcludeStudent> concludeStudents) {

        for (ConcludeStudent record : concludeStudents) {
            ConcludeStudentExample example = new ConcludeStudentExample();
            example.createCriteria().andSnoEqualTo(record.getSno()).andConcludeIdEqualTo(record.getConcludeId()).andDeletedEqualTo(false);
            concludeStudentMapper.updateByExampleSelective( record, example);
        }
    }

    public ConcludeStudent get(Long id) {

        ConcludeStudentExample example = new ConcludeStudentExample();
        example.createCriteria().andIdEqualTo(id).andDeletedEqualTo(false);
        List<ConcludeStudent> concludeStudents = concludeStudentMapper.selectByExample(example);
        if(CollectionUtil.isNotEmpty(concludeStudents)){

            return concludeStudents.get(0);
        }
        return null;
    }
}
